//CSP-J 2019 普及组 T2

#include <iostream>
#include <cstdio>

#define MAXN 100010

using namespace std;

struct ticket{
	int price;
	int last;
	bool flag; //用过为False，没用过为True
}; 
ticket tic[MAXN];

int n;	int tail = 1;	int head = 0;
int ans = 0;

int main(){
	cin >> n;
	int option,price,time;
	for (int i = 1; i <= n; i++){
		//cin >> b.option >> b.price >> b.time;
		scanf("%d%d%d", &option, &price, &time);
		//地铁
		if (option == 0) {
			ans += price;
			tic[tail].last = time + 45;		tic[tail].flag = true;		tic[tail].price = price;
			tail++;
		}
		//公交车
		else {
			while ((head < tail) && (tic[head].last < time)) {
					head++;	
			}
			bool yes = false;
			for (int j = head; j < tail; j++){
				if ((tic[j].price >= price) && (tic[j].flag == true)) {
					yes = true;  tic[j].flag = false;	break;
				}
			}
			if (!yes) {
					ans += price;
				}
		}
	}
	cout << ans << endl;
	return 0;
}